﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Windows.Input;
using Bon_It.Model;
using Bonit_It.ViewModel.Base;

namespace Bonit_It.ViewModel
{
    using ViewModel = Bonit_It.ViewModel.Base.ViewModel;

    public partial class MainViewModel : Base.ViewModel
    {
        private ObservableCollection<Base.ViewModel> _ws;
        /// <summary>
        /// The workspaces
        /// </summary>
        public ObservableCollection<ViewModel> WorkSpaces { get { return _ws; } set { _ws = value; }}

        public MainViewModel()
        {
            WorkSpaces = new ObservableCollection<ViewModel>();
            DisplayName = "Hauptmaske";
            AssignCommands();
            this.ShowSplash();
        }

        private void ShowSplash()
        {
            SplashScreenViewModel model = new SplashScreenViewModel();
            model.RefreshLookups();
            model.Dispose();
        }


        #region Overrides of ViewModel

        


        public override void RefreshLookups()
        {
            
        }

        public override void CleanUp()
        {
            WorkSpaces.Clear();
            WorkSpaces = null;
        }

        public override bool CanDelete(object argument)
        {
            return true;
        }

        public override bool CanSave(object argument)
        {
            return false;
        }

        public override bool CanCreate(object argument)
        {
            return true;
        }


       


        #endregion
        void SetActiveWorkspace(Base.ViewModel workspace)
        {
            Debug.Assert(this.WorkSpaces.Contains(workspace));

            workspace.RequestClose += OnWorkspaceRequestClose;
            workspace.AttachWorkspace += new AttachWorkspaceHandler(workspace_AttachWorkspace);
            ICollectionView collectionView = CollectionViewSource.GetDefaultView(this.WorkSpaces);
            if (collectionView != null)
                collectionView.MoveCurrentTo(workspace);
        }

        void workspace_AttachWorkspace(object sender, ViewModel viewModel)
        {
            WorkSpaces.Add(viewModel);
            SetActiveWorkspace(viewModel);
        }

        void OnWorkspaceRequestClose(object sender, EventArgs e)
        {
            ViewModel workspace = sender as ViewModel;
            workspace.Dispose();
            this.WorkSpaces.Remove(workspace);
        }
    
    }
}
